package com.appology.chuck;

import java.io.IOException;
import javax.servlet.http.*;

import com.appology.jokes.Joke;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.users.User;

@SuppressWarnings("serial")
public class BetterthanchuckServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		UserService userService = UserServiceFactory.getUserService();
		
        String thisURL = request.getRequestURI();
	    if (request.getParameter("addJokes") != null && request.getParameter("addJokes").equalsIgnoreCase("true")){
    		Joke.getInstance().AddItemsToDB();
        }
        if (request.getUserPrincipal() != null) {           
            response.getWriter().println(Joke.getInstance().getRandom(userService.getCurrentUser().getNickname()));
            
        } else {
        	User user = userService.getCurrentUser();
        	
        	String joke = Joke.getInstance().getRandom(null);
        	response.getWriter().println(joke);

        }
	}
}
